package cn.itsource.celljava.system.logininfo.service.impl;

import cn.itsource.celljava.config.BussnisEptional;
import cn.itsource.celljava.system.employee.domain.Employee;
import cn.itsource.celljava.system.employee.domain.EmployeeBo;
import cn.itsource.celljava.system.logininfo.domain.Logininfo;
import cn.itsource.celljava.system.logininfo.mapper.LogininfoMapper;
import cn.itsource.celljava.system.logininfo.service.ILogininfoService;
import cn.itsource.celljava.base.service.impl.BaseServiceImpl;
import cn.itsource.celljava.utils.AjaxResult;
import cn.itsource.celljava.utils.MD5Utils;
import cn.itsource.celljava.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

 
@Service
public class LogininfoServiceImpl extends BaseServiceImpl<Logininfo> implements ILogininfoService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void change(Long admin_id,Integer state)
    {
        logininfoMapper.change(admin_id,state);
    }

    @Override
    public EmployeeBo findToLogin(Logininfo logininfo) {
        if (logininfo.getType()==null){
            throw new BussnisEptional("非法访问");
        }
        Logininfo emp = logininfoMapper.findToLogin(logininfo.getUsername());
        if (emp==null){
            throw new BussnisEptional("用户名不存在");
        }
        String salt = emp.getSalt();
        String md5 = MD5Utils.encrypByMd5(logininfo.getPassword() + salt);
        if (!md5.equals(emp.getPassword())){
            throw new BussnisEptional("用户名或密码错误");
        }
        //用户成功登录
        EmployeeBo employeeBo = new EmployeeBo();
        BeanUtils.copyProperties(emp,employeeBo);
        return employeeBo;
    }
}
